﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SmartSpace.UI.Silverlight.Dynamic.DynamicRef;
using System.Text;

namespace SmartSpace.UI.Silverlight.Dynamic.Controls
{
    /// <summary>
    /// 选择组织机构控件
    /// 可选择所有组织机构或者当前机构下所有机构
    /// </summary>
    public partial class OrgListControl : PMFDataControl
    {
        private bool m_isSubOnly = false;
        private string m_userName;

        private List<Org> _orgList;

        public OrgListControl(bool subOnly, string userName)
        {
            m_isSubOnly = subOnly;
            m_userName = userName;

            InitializeComponent();
        }

        #region override

        public override void UpdateSource()
        {
            if (_orgList != null)
            {
                StringBuilder idBuilder = new StringBuilder();
                StringBuilder textBuilder = new StringBuilder();
                foreach (Org org in _orgList)
                {
                    idBuilder.AppendFormat("{0},", org.Id.ToString("D"));
                    textBuilder.AppendFormat("{0},", org.Caption);
                }
                if (idBuilder.Length > 0)
                {
                    idBuilder = idBuilder.Remove(idBuilder.Length - 1, 1);
                    textBuilder = textBuilder.Remove(textBuilder.Length - 1, 1);
                }
                DataSource.Value = idBuilder.ToString();
                DataSource.Text = textBuilder.ToString();
            }

            this.GetBindingExpression(PMFDataControl.DataSourceProperty).UpdateSource();
        }

        protected override void OnIsReadOnlyChanged(bool isReadOnly)
        {
            if (isReadOnly)
            {
                m_btnBrowser.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                m_btnBrowser.Visibility = System.Windows.Visibility.Visible;
            }
        }

        protected override void OnDataSourceChanged(FieldInstance dataSource)
        {
            if (!string.IsNullOrEmpty(dataSource.Text))
            {
                this.m_textOrgName.Text = dataSource.Text;
                string[] ids = dataSource.Value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                string[] names = dataSource.Text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                _orgList = new List<Org>();
                for (int index = 0; index < ids.Length; index++)
                {
                    Org org = new Org();
                    org.Id = new Guid(ids[index]);
                    org.Caption = names[index];
                    _orgList.Add(org);
                }
            }
        }

        #endregion

        /// <summary>
        /// 选择组织机构按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnButtonBrowserClick(object sender, RoutedEventArgs e)
        {
            SelectOrgWindow window = new SelectOrgWindow(m_isSubOnly, m_userName, _orgList);
            window.Closed += new EventHandler(OnWindowClosed);
            window.Show();
        }

        void OnWindowClosed(object sender, EventArgs e)
        {
            SelectOrgWindow window = sender as SelectOrgWindow;
            if (window.DialogResult == true)
            {
                _orgList = window.SelectedOrgs;

                UpdateSelectedText();
            }
        }

        private void UpdateSelectedText()
        {
            StringBuilder textBuilder = new StringBuilder();
            for (int index = 0; index < _orgList.Count; index++)
            {
                Org org = _orgList[index];
                textBuilder.AppendFormat("{0}", org.Caption);
                if (index < _orgList.Count - 1)
                {
                    textBuilder.Append(",");
                }
            }

            m_textOrgName.Text = textBuilder.ToString();
        }
    }
}
